home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / VerticalSlider.java < prev    next >
Text File  |  1998-08-21  |  12KB  |  420 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Event;
  4. import java.awt.Graphics;
  5. import java.awt.Color;
  6. import java.beans.PropertyVetoException;
  7. import java.beans.PropertyChangeListener;
  8. import java.beans.VetoableChangeListener;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseMotionListener;
  12.  
  13. //     02/15/97    RKM    Added validate - Fixes problem where calls to setMinValue & setMaxValue
  14. //                    would not work if called after reshape
  15. //     02/27/97    RKM    Integrated Scott's change to use the background color of the component
  16. //    05/30/97    MSH    Updated to support Java 1.1
  17. //  06/11/97    MSH Removed postEvent() and added correct listener registration fuctions
  18. //    07/17/97    LAB    Added add/removeNotify for event listener registration.  Separated event handling
  19. //                    innerclass into two innerclasses.  Made the bound and constrained listener
  20. //                    registration functions call it's super.  Changed setTickStyle to call it's super.
  21. //                    Moved doMove() into Slider since it was the same as HorizontalSlider:doMove().
  22. //                    Moved Action Listener/Event stuff into the base class. In general, moved common
  23. //                    things to the base class.
  24. //  07/24/97    CAR marked field transient as needed
  25. //                  implemented readObject
  26. //  08/12/97    RKM Removed property changed and veto support - it was all moved into the
  27. //                    base class and was no longer accessed from this class
  28. //                    Remove unnecessary clipRect in paint call
  29. //    10/05/97    LAB    Made calculations in do_reshape use floats instead of ints to avoid rounding
  30. //                    errors with large numbers of ticks (Addresses Mac Bug #7591).
  31.  
  32. /**
  33.  * This component is used to select one value
  34.  * from a continuous range of values. It has a movable thumb in front of a
  35.  * gauge with ticks marks on it.
  36.  * <p>
  37.  * @see symantec.itools.awt.HorizontalSlider
  38.  * @version 1.1, July 17, 1997
  39.  * @author Symantec
  40.  */
  41. public class VerticalSlider extends Slider
  42. {
  43.    /**
  44.      * Constructs a default VerticalSlider. The ticks
  45.      * are drawn on both sides of the gauge with a frequency of 1.
  46.      * The minimum value is 1. The maximum value is 10. The
  47.      * border is shown.
  48.      */
  49.     public VerticalSlider()
  50.     {
  51.         this.thumb    = new VerticalSliderThumbBoth();
  52.         width        = 175;
  53.     }
  54.  
  55.     /**
  56.      * Sets the current slider tick mark style.
  57.      *
  58.      * @param style the new tick mark style, one of TICK_LEFT, TICK_RIGHT, or TICK_BOTH
  59.      * @see symantec.itools.awt.Slider#getTickStyle
  60.      * @see Slider#TICK_LEFT
  61.      * @see Slider#TICK_RIGHT
  62.      * @see Slider#TICK_BOTH
  63.      * @exception PropertyVetoException
  64.      * if the specified property value is unacceptable
  65.      */
  66.     public void setTickStyle(int style) throws PropertyVetoException
  67.     {
  68.         if (this.style != style)
  69.         {
  70.             super.setTickStyle(style);
  71.  
  72.             switch (style)
  73.             {
  74.                 case TICK_LEFT :
  75.                     thumb = new VerticalSliderThumbLeft();
  76.                     break;
  77.                 case TICK_RIGHT :
  78.                     thumb = new VerticalSliderThumbRight();
  79.                     break;
  80.                 default :
  81.                     thumb = new VerticalSliderThumbBoth();
  82.                     break;
  83.             }
  84.             repaint();
  85.         }
  86.     }
  87.  
  88.     /**
  89.      * Tells this component that it has been added to a container.
  90.      * This is a standard Java AWT method which gets called by the AWT when
  91.      * this component is added to a container. Typically, it is used to
  92.      * create this component's peer.
  93.      *
  94.      * It has been overridden here to handle registering event listeners.
  95.      *
  96.      * @see #removeNotify
  97.      */
  98.     public synchronized void addNotify()
  99.     {
  100.         super.addNotify();
  101.  
  102.         //Hook up listeners
  103.         if (mouse == null)
  104.         {
  105.             mouse = new Mouse();
  106.             addMouseListener(mouse);
  107.         }
  108.         if (mouseMotion == null)
  109.         {
  110.             mouseMotion = new MouseMtn();
  111.             addMouseMotionListener(mouseMotion);
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * Tells this component that it is being removed from a container.
  117.      * This is a standard Java AWT method which gets called by the AWT when
  118.      * this component is removed from a container. Typically, it is used to
  119.      * destroy the peers of this component and all its subcomponents.
  120.      *
  121.      * It has been overridden here to handle registering event listeners.
  122.      *
  123.      * @see #addNotify
  124.      */
  125.     public synchronized void removeNotify()
  126.     {
  127.         //Unhook listeners
  128.         if (mouse != null)
  129.         {
  130.             removeMouseListener(mouse);
  131.             mouse = null;
  132.         }
  133.         if (mouseMotion != null)
  134.         {
  135.             removeMouseMotionListener(mouseMotion);
  136.             mouseMotion = null;
  137.         }
  138.  
  139.         super.removeNotify();
  140.     }
  141.  
  142.     /**
  143.      * This is the Mouse Event handling innerclass.
  144.      */
  145.     class Mouse extends java.awt.event.MouseAdapter implements java.io.Serializable
  146.     {
  147.         /**
  148.          * Handles Mouse Pressed events
  149.          * @param e the MouseEvent
  150.          */
  151.         public void mousePressed(MouseEvent e)
  152.         {
  153.             moveThumb( e.getY(), true );
  154.         }
  155.     }
  156.  
  157.     /**
  158.      * This is the MouseMotion Event handling innerclass.
  159.      */
  160.     class MouseMtn extends java.awt.event.MouseMotionAdapter implements java.io.Serializable
  161.     {
  162.         /**
  163.          * Handles Mouse Dragged events
  164.          * @param e the MouseEvent
  165.          */
  166.         public void mouseDragged(MouseEvent e)
  167.         {
  168.             moveThumb( e.getY(), false );
  169.         }
  170.     }
  171.  
  172.     protected void do_reshape(int w, int h)
  173.     {
  174.         float hb = BORDER_X;
  175.         float vb = BORDER_Y;
  176.  
  177.         if (w < hb)
  178.             hb = w / 4;
  179.  
  180.         if (h < vb)
  181.             vb = h / 4;
  182.  
  183.         float x0 = hb;
  184.         float x1 = w - hb;
  185.         float y0 = vb;
  186.         float y1 = h - vb;
  187.  
  188.         if (x0 == 0)
  189.             x0 = 1;
  190.  
  191.         if (x1 == 0)
  192.             x1 = 1;
  193.  
  194.         if (y0 == 0)
  195.             y0 = 1;
  196.  
  197.         if (y1 == 0)
  198.             y1 = 1;
  199.  
  200.         float n = (max - min) / freq + 1;
  201.  
  202.         tick = new SliderTick[(int)n];
  203.  
  204.         float vs = (y1 - y0) / (n - 1), ch;
  205.  
  206.         for (int i = 0; i < n; ++i)
  207.         {
  208.             ch = i * vs;
  209.             tick[i] = new SliderTick((int)x0, (int)x1, (int)(y0 + ch), (int)ch);
  210.         }
  211.  
  212.         thumb.resize((int)(x1 - x0 - TICK_WIDTH - 1), (int)(vs / 2));
  213.     }
  214.  
  215.     /**
  216.      * Paints this component using the given graphics context.
  217.      * This is a standard Java AWT method which typically gets called
  218.      * by the AWT to handle painting this component. It paints this component
  219.      * using the given graphics context. The graphics context clipping region
  220.      * is set to the bounding rectangle of this component and its [0,0]
  221.      * coordinate is this component's top-left corner.
  222.      *
  223.      * @param g the graphics context used for painting
  224.      * @see java.awt.Component#repaint
  225.      * @see java.awt.Component#update
  226.      */
  227.     public void paint(Graphics g)
  228.     {
  229.         super.paint(g);
  230.         
  231.         if (tick.length == 0)
  232.             return;
  233.  
  234.         thumb.draw(g, tick[curPos]);
  235.  
  236.         if (prevPos != curPos)
  237.             thumb.clip(g, tick[prevPos]);
  238.  
  239.         g.setColor(getBackground());
  240.         g.fillRect(0, 0, width, height);
  241.  
  242.         g.setColor(Color.black);
  243.  
  244.         int x, y0, y1, w = width - 1, h = height - 1;
  245.         boolean end;
  246.  
  247.         if (showBorder)
  248.             g.drawRect(0, 0, w, h);
  249.  
  250.         for (int i = 0; i < tick.length; ++i)
  251.         {
  252.             end = i == 0 || i == tick.length - 1;
  253.  
  254.             SliderTick t = tick[i];
  255.  
  256.             if (style == TICK_LEFT || style == TICK_BOTH)
  257.                 g.drawLine(t.a + (end ? 0 : 1), t.c, t.a + TICK_WIDTH, t.c);
  258.  
  259.             if (style == TICK_RIGHT || style == TICK_BOTH)
  260.                 g.drawLine(t.b - TICK_WIDTH, t.c, t.b - (end ? 0 : 1), t.c);
  261.         }
  262.         
  263.         SliderTick t = tick[0];
  264.  
  265.         x  = (t.b + t.a) / 2;
  266.         y0 = t.c - 5;
  267.         y1 = tick[tick.length - 1].c + 5;
  268.  
  269.         g.drawLine(x, y0, x, y1);
  270.  
  271.         g.setColor(Color.gray);
  272.         g.drawLine(x - 1, y1 + 1, x - 1, y0 - 1);
  273.         g.drawLine(x - 1, y0 - 1, x + 1, y0 - 1);
  274.  
  275.         g.setColor(Color.lightGray);
  276.         g.drawLine(x + 1, y0, x + 1, y1 + 1);
  277.         g.drawLine(x + 1, y1 + 1, x, y1 + 1);
  278.  
  279.         g.setColor(Color.white);
  280.         g.drawLine(x + 2, y0 - 1, x + 2, y1 + 2);
  281.         g.drawLine(x + 2, y1 + 2, x - 1, y1 + 2);
  282.         
  283.         //???RKM??? This cannot work
  284.         g.clipRect(0, 0, width, height);
  285.  
  286.         thumb.draw(g, tick[curPos]);
  287.  
  288.         prevPos = curPos;
  289.     }
  290.     
  291.     private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
  292.         in.defaultReadObject();
  293.         
  294.         //???RKM??? I do not believe this is necessary anymore, but can't test so I'll leave it in
  295.         do_reshape(width, height);
  296.     }
  297.  
  298.     /**
  299.      * Length of the gauge ticks in pixels.
  300.      */
  301.     protected static final int TICK_WIDTH   = 4;
  302.  
  303.     /**
  304.      * The command name of the action event fired by this component.
  305.      */
  306.     protected String actionCommand = "VSliderMoved";
  307.  
  308.     private VerticalSliderThumb thumb;
  309.     private Mouse        mouse            = null;
  310.     private MouseMtn    mouseMotion        = null;
  311. }
  312.  
  313. abstract class VerticalSliderThumb implements java.io.Serializable
  314. {
  315.     void resize(int width, int height)
  316.     {
  317.         x = (this.width = width) - VerticalSlider.TICK_WIDTH - 1 - 1;
  318.         y = (this.height = height) / 2;
  319.     }
  320.  
  321.     abstract void draw(Graphics g, SliderTick t);
  322.  
  323.     protected void draw(int x0, int y0, int x1, int y1)
  324.     {
  325.         g.drawLine(t.a + x0 + VerticalSlider.TICK_WIDTH + 1, t.c + y0,
  326.                    t.a + x1 + VerticalSlider.TICK_WIDTH + 1, t.c + y1);
  327.     }
  328.  
  329.     protected void initDraw(Graphics g, SliderTick t)
  330.     {
  331.         this.g = g;
  332.         this.t = t;
  333.  
  334.         g.setColor(Color.lightGray);
  335.         g.fillRect(t.a + 2 + VerticalSlider.TICK_WIDTH + 1, t.c - y + 1, x - 2, height - 2);
  336.  
  337.         g.setColor(Color.white);
  338.     }
  339.  
  340.     void clip(Graphics g, SliderTick t)
  341.     {
  342.         g.clipRect(t.a, t.c - height / 2, width + 1, height + 1);
  343.     }
  344.  
  345.     protected int x;
  346.     protected int y;
  347.     protected int width;
  348.     protected int height;
  349.     transient protected Graphics g;
  350.     transient protected SliderTick t;
  351. }
  352.  
  353.  
  354. class VerticalSliderThumbBoth extends VerticalSliderThumb
  355. {
  356.     void draw(Graphics g, SliderTick t)
  357.     {
  358.         super.initDraw(g, t);
  359.  
  360.         draw(x, -y, 1, -y);
  361.         draw(1, -y, 1, y);
  362.  
  363.         g.setColor(Color.black);
  364.         draw(x, -y, x, y);
  365.         draw(x, y, 1, y);
  366.  
  367.         g.setColor(Color.gray);
  368.         draw(x - 1, -y + 1, x - 1, y - 1);
  369.         draw(x - 1, y - 1, 2, y - 1);
  370.     }
  371. }
  372.  
  373.  
  374. class VerticalSliderThumbLeft extends VerticalSliderThumb
  375. {
  376.     void draw(Graphics g, SliderTick t)
  377.     {
  378.         super.initDraw(g, t);
  379.  
  380.         int a = x / 4;
  381.  
  382.         draw(x, -y, a, -y);
  383.         draw(a, -y, 1, 0);
  384.  
  385.         g.setColor(Color.black);
  386.         draw(1, 0, a, y);
  387.         draw(a, y, x, y);
  388.         draw(x, y, x, -y);
  389.  
  390.         g.setColor(Color.gray);
  391.         draw(2, 0, a, y - 1);
  392.         draw(a, y - 1, x - 1, y - 1);
  393.         draw(x - 1, y - 1, x - 1, -y + 1);
  394.     }
  395. }
  396.  
  397.  
  398. class VerticalSliderThumbRight extends VerticalSliderThumb
  399. {
  400.     void draw(Graphics g, SliderTick t)
  401.     {
  402.         super.initDraw(g, t);
  403.  
  404.         int a = width - VerticalSlider.TICK_WIDTH - 1 - 1 - x / 4;
  405.  
  406.         draw(1, y, 1, -y);
  407.         draw(1, -y, a, -y);
  408.         draw(a, -y, x - 1, 0);
  409.  
  410.         g.setColor(Color.black);
  411.         draw(x, 0, a, y);
  412.         draw(a, y, 1, y);
  413.  
  414.         g.setColor(Color.gray);
  415.         draw(x - 1, 0, a, y - 1);
  416.         draw(a, y - 1, 2, y - 1);
  417.     }
  418. }
  419.  
  420.